<template>
  <div>
  <van-collapse :value="activeNames" @change="onChange">
    <van-collapse-item v-for="(a,index) in item" :key="index"  :name="index+1">
      <view slot="title"><div class="test">
      <span>{{a.simplename}}</span>
      <van-icon name="more-o" size="20" @click.stop="getMore(a,index)"/>
      </div>
      </view>

    </van-collapse-item>

  </van-collapse>
 <van-dialog id="van-dialog" />
  <testModeBox ref="testModeBox" @changeDom="changeDoms"></testModeBox>
  </div>
</template>

<script>
  import Collapse from "./Collapse"
   import modelBox from './modelBox'
   import testModeBox from "./testModeBox.vue"
   import Dialog from '../../static/vant-weapp/dist/dialog/dialog';
  export default {
    name: "Collapse2",
    props:['item'],
    data(){
      return {
        activeNames: ['1']
      }
    },
    components:{
      Collapse,
      modelBox,
      testModeBox,

    },
    methods:{
      changeDoms(){
        this.$refs.testModeBox.$parent.$parent.$parent.$parent.$parent.changeDoms()
      },
      getMore(item,index){
        let that=this;
        wx.showActionSheet({
          itemList: ['添加', '编辑', '删除','详情'],
          success (res) {
            console.log(res.tapIndex)
            if(res.tapIndex==0){
               that.$refs.testModeBox.init('添加分类',item.id)
            }else if(res.tapIndex==1){
                that.$refs.testModeBox.init('编辑',item.id,item)
            }else if(res.tapIndex==2){
              Dialog.confirm({
                title: '删除提示',
                message: '是否确定删除?'
              }).then(() => {
               that.deleteItem(item.id)
                // on confirm
              }).catch(() => {
                // on cancel
              });

            }else if(res.tapIndex==3){
                  wx.navigateTo({ url: '/pages/createProductDetail/main/id='+item.id })
            }


          },
          fail (res) {
            console.log(res.errMsg)
          }
        })
        // this.description="所选类型"+item.simplename
        // this.data[index].isShow=true;
        // this.$set(this.data,this.data[index].isShow,true)
        // this.show=true;
      },

      deleteItem(id){
        let that=this;
        this.$http.request({
          method:"get",
          url:"/api/category/delete",
          body:{
            categoryId:id
          }
        }).then((data)=>{
          if(data.code==200){
            wx.showToast({
              title: '删除成功!',
              icon: 'success',
              duration: 2000,
              success:()=>{
                that.changeDoms()
              }
            })
          }else{
            wx.showToast({
              title: `${data.message}`,
              icon: 'none',
              duration: 2000,

            })
          }
        })
      },
      onChange(e){
        this.activeNames=e.mp.detail
      }
    }
  }
</script>

<style scoped>
 .test{
     display: flex;
     justify-content: space-between;
     align-items: center;
   }
</style>
